package com.wva.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;

/**
 * @Author: hong
 * @Date: 2025/2/17 15:53:44
 */
public class MQAutoConfigure {
    /**
     * 消息转换器
     * @return
     */
    @Bean
    MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    //处理失败的存放队列
    @Bean
    TopicExchange errorTopic() {
        return new TopicExchange("error.topic");
    }
    @Bean
    Queue errorQueue() {
        return new Queue("error.queue");
    }
    @Bean
    Binding errorQueueBinding(Queue errorQueue, TopicExchange errorTopic) {
        return BindingBuilder
                .bind(errorQueue)
                .to(errorTopic)
                .with("error");
    }

    /**
     * 项目中所有处理失败的消息统一投放到这里，后续由专业人工一一处理！
     * @param rabbitTemplate
     * @return
     */
    @Bean
    MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, "error.topic", "error");
    }
}
